// src/router/index.js
import { createRouter, createWebHistory,createWebHashHistory } from 'vue-router'
import MainLayout from '@/layouts/MainLayout.vue'
import WalletPage from '@/views/Wallet.vue'
import PurchesPage from '@/views/Purches.vue'
import GroupControl from '@/views/GroupControl.vue'
import PaymentResult from '@/views/PaymentResult.vue'
import MyPhone from '@/views/MyPhone.vue'
import Cloud_disk from '@/views/Cloud_disk.vue'
import RoomPage from '@/views/Room.vue'
import RegisterPage from '@/views/RegisterPage.vue'
import Automated_Operation from '@/views/Automated_Operation.vue'
import Automatically_Publish_videos from '@/views/Automated_Operation/Automatically_Publish_videos.vue'
import Material_Center from '@/views/Automated_Operation/Material_Center.vue'
import Account_Management from '@/views/Automated_Operation/Account_Management.vue'
import Agency_Index from '@/views/Agency_module/Agency_Index.vue'
import Look_Task from '@/views/Automated_Operation/Look_Task.vue'
import { getCurrentInstance } from 'vue'
import store from '../store'
const routes = [
	{
		path: '/register',
		name: 'Register',
		component: RegisterPage,
		meta: { requiresAuth: false } // 添加路由元信息，标记不需要认证
	},

	{
		path: '/payment/result',
		name: 'PaymentResult',
		component: PaymentResult,
		meta: { requiresAuth: false } // 添加路由元信息，标记不需要认证
	},
	{
		path: "/room",
		name: 'Room',
		component: RoomPage
	},
	{
		path: "/groupControl",
		name: 'GroupControl',
		component: GroupControl,
		meta: {
			requiresAuth: false// 添加路由元信息，标记不需要认证
			// NoNeedHome: true // 添加该字段，表示不需要home模板
		}
	},
	{
		path: '/',
		component: MainLayout,
		meta: { requiresAuth: false }, // 需要认证
		children: [
			{
				path: '',
				redirect: { name: 'PurchesPage', params: { id: '0' } } // 使用命名路由更安全
			},
			{
				path: 'wallet',
				name: 'Wallet',
				component: WalletPage
			},
			// 其它登录后的页面...
			{
				path: 'purches/:id?',
				name: 'PurchesPage',
				component: PurchesPage,
				props: (routes : any) => ({
					id: routes.params.id || '0'
				})
				//meta: { requiresAuth: false } // 添加路由元信息，标记不需要认证
			},
			{
				path: '/my_phone',
				name: 'MyPhone',
				component: MyPhone,
			},
			{
				path: '/cloud_disk',
				name: 'Cloud_disk',
				component: Cloud_disk,
			},
			{
				path: '/automated_operation',
				name: 'Automated_Operation',
				component: Automated_Operation
			},
			{
				path: '/automated_operation/Automatically_Publish_videos',
				name: 'Automatically_Publish_videos',
				component: Automatically_Publish_videos,
				meta: {
					requiresAuth: false,// 添加路由元信息，标记不需要认证
					activeMenu: '/automated_operation'
				}
			},
			{
				path: '/automated_operation/Material_Center',
				name: 'Material_Center',
				component: Material_Center,
				meta: {
					requiresAuth: false,// 添加路由元信息，标记不需要认证
					activeMenu: '/automated_operation'
				}
			},
			{
				path: '/automated_operation/Account_Management',
				name: 'Account_Management',
				component: Account_Management,
				meta: {
					requiresAuth: false,// 添加路由元信息，标记不需要认证
					activeMenu: '/automated_operation'
				}
			},
			{
				path: '/automated_operation/Look_Task',
				name: 'Look_Task',
				component: Look_Task,
				meta: {
					requiresAuth: false,// 添加路由元信息，标记不需要认证
					activeMenu: '/automated_operation'
				}
			},
			{
				path: '/Agency_module/Agency_Index',
				name: 'Agency_Index',
				component: Agency_Index
			},
			
		]
	},
	{
		path: '/:pathMatch(.*)*',
		redirect: '/purches'
	}
]

const router = createRouter({
	history: createWebHashHistory(),
	routes
})

// // 添加全局路由守卫
router.beforeEach((to, from, next) => {
	const isAuthenticated = !!localStorage.getItem('token')
	// 确保 to 对象存在
	if (!to) {
		console.error('无效的路由对象')
		return next('/error') // 跳转到错误页
	}
	const instance = getCurrentInstance()
	// 初始化 meta
	to.meta = to.meta || { requiresAuth: true }
	if (to.meta.requiresAuth && !isAuthenticated) {
		// 如果是需要认证的页面且未登录，弹出登录弹框
		const openLoginDialog = instance?.appContext.config.globalProperties.$openLogin_dialog
		if (openLoginDialog) {
			openLoginDialog()
		}
	}
	//防止跳转注册页面时登录弹框仍然显示
	if (to.path == '/register') {
		store.commit('Login_dialog_hide')
	}
	next()
})

export default router